#!/usr/bin/python3
# by Sun Smallwhite <niasw@pku.edu.cn>(https://github.com/niasw)

def cmbClusByMat(clusters,linkmat):
  '''
# manually combine two clusters, if (linkmat[j][k]>0.) then combine clusters[j] and clusters[k]
# input:
#  simplified clusters, link matrix (in ndarray type)
# output:
#  combined simplified clusters
  '''
  clusters=clusters.copy();
  (cmbrow,cmbcol)=linkmat.nonzero();
  cmbrow=cmbrow.tolist();
  cmbcol=cmbcol.tolist();
  lgth=len(cmbrow);
  for it in range(0,lgth):
    if (cmbrow[it]!=cmbcol[it]):
      clusters[cmbrow[it]].extend(clusters[cmbcol[it]]);
      clusters[cmbcol[it]]=[];
  cur=0;
  while (cur<len(clusters)):
    if (len(clusters[cur])==0):
      clusters.pop(cur);
    else:
      clusters[cur].sort();
      cur+=1;
  return clusters;

